<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
            <el-form-item label="任务编号" prop="taskCode">
                <el-input
                    v-model="queryParams.taskCode"
                    placeholder="请输入任务编号"
                    clearable
                    size="small"
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
<!--            <el-form-item label="任务完成时间" prop="finishTime">-->
<!--                <el-input-->
<!--                    v-model="queryParams.finishTime"-->
<!--                    placeholder="请输入任务完成时间"-->
<!--                    clearable-->
<!--                    size="small"-->
<!--                    @keyup.enter.native="handleQuery"-->
<!--                />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="下载地址" prop="downloadUrl">-->
<!--                <el-input-->
<!--                    v-model="queryParams.downloadUrl"-->
<!--                    placeholder="请输入下载地址"-->
<!--                    clearable-->
<!--                    size="small"-->
<!--                    @keyup.enter.native="handleQuery"-->
<!--                />-->
<!--            </el-form-item>-->
<!--            <el-form-item label="下载次数" prop="downloadCount">-->
<!--                <el-input-->
<!--                    v-model="queryParams.downloadCount"-->
<!--                    placeholder="请输入下载次数"-->
<!--                    clearable-->
<!--                    size="small"-->
<!--                    @keyup.enter.native="handleQuery"-->
<!--                />-->
<!--            </el-form-item>-->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="excelExportTaskList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <!--            <el-table-column label="主键" align="center" prop="id"/>-->
            <el-table-column label="任务编号" align="center" prop="taskCode"/>
            <el-table-column label="任务类型" align="center" prop="taskType"/>
            <!--            <el-table-column label="任务参数" align="center" prop="taskParam"/>-->
            <el-table-column label="任务状态" align="center" prop="status">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.biz_excel_export_task_status" :value="scope.row.status"/>
                </template>
            </el-table-column>
            <el-table-column label="任务完成时间" align="center" prop="finishTime">
                <template slot-scope="scope">
                   {{parseTime(scope.row.finishTime)}}
                </template>
            </el-table-column>
<!--            <el-table-column label="下载地址" align="center" prop="downloadUrl"/>-->
            <el-table-column label="下载次数" align="center" prop="downloadCount"/>
            <el-table-column label="文件清理时间" align="center" prop="clearTime"/>
            <!--            <el-table-column label="用户ID" align="center" prop="userId"/>-->
            <!--            <el-table-column label="项目ID" align="center" prop="projectId"/>-->
            <el-table-column label="任务结果" align="center" prop="content"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                        size="mini"
                        type="text"
                        @click="handleDownload(scope.row)"
                    >下载
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />

        <!-- 添加或修改excel导出任务对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="任务编号" prop="taskCode">
                    <el-input v-model="form.taskCode" placeholder="请输入任务编号"/>
                </el-form-item>
                <el-form-item label="任务参数" prop="taskParam">
                    <el-input v-model="form.taskParam" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
                <el-form-item label="任务完成时间" prop="finishTime">
                    <el-input v-model="form.finishTime" placeholder="请输入任务完成时间"/>
                </el-form-item>
                <el-form-item label="下载地址" prop="downloadUrl">
                    <el-input v-model="form.downloadUrl" placeholder="请输入下载地址"/>
                </el-form-item>
                <el-form-item label="下载次数" prop="downloadCount">
                    <el-input v-model="form.downloadCount" placeholder="请输入下载次数"/>
                </el-form-item>
                <el-form-item label="文件清理时间" prop="clearTime">
                    <el-input v-model="form.clearTime" placeholder="请输入文件清理时间"/>
                </el-form-item>
                <el-form-item label="用户ID" prop="userId">
                    <el-input v-model="form.userId" placeholder="请输入用户ID"/>
                </el-form-item>
                <el-form-item label="项目ID" prop="projectId">
                    <el-input v-model="form.projectId" placeholder="请输入项目ID"/>
                </el-form-item>
                <el-form-item label="${comment}" prop="remark">
                    <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listExcelExportTask,
    getExcelExportTask,
    delExcelExportTask,
    addExcelExportTask,
    updateExcelExportTask
} from '@/api/excel/excelExportTask'
export default {
    dicts: ['biz_excel_export_task_status'],
    name: 'ExcelExportTask',
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // excel导出任务表格数据
            excelExportTaskList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                taskCode: null,
                taskType: null,
                taskParam: null,
                status: null,
                finishTime: null,
                downloadUrl: null,
                downloadCount: null,
                clearTime: null,
                userId: null,
                projectId: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                taskCode: [
                    { required: true, message: '任务编号不能为空', trigger: 'blur' }
                ],
                taskType: [
                    { required: true, message: '任务类型不能为空', trigger: 'change' }
                ],
                taskParam: [
                    { required: true, message: '任务参数不能为空', trigger: 'blur' }
                ],
                status: [
                    { required: true, message: '任务状态不能为空', trigger: 'blur' }
                ],
                finishTime: [
                    { required: true, message: '任务完成时间不能为空', trigger: 'blur' }
                ],
                downloadUrl: [
                    { required: true, message: '下载地址不能为空', trigger: 'blur' }
                ],
                downloadCount: [
                    { required: true, message: '下载次数不能为空', trigger: 'blur' }
                ],
                clearTime: [
                    { required: true, message: '文件清理时间不能为空', trigger: 'blur' }
                ],
                userId: [
                    { required: true, message: '用户ID不能为空', trigger: 'blur' }
                ],
                projectId: [
                    { required: true, message: '项目ID不能为空', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {

        getList() {
            this.loading = true
            listExcelExportTask(this.queryParams).then(response => {
                this.excelExportTaskList = response.rows
                this.total = response.total
                this.loading = false
            })
        },

        cancel() {
            this.open = false
            this.reset()
        },

        reset() {
            this.form = {
                id: null,
                taskCode: null,
                taskType: null,
                taskParam: null,
                status: '0',
                finishTime: null,
                downloadUrl: null,
                downloadCount: null,
                clearTime: null,
                userId: null,
                projectId: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            }
            this.resetForm('form')
        },

        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },

        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },

        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },

        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加excel导出任务'
        },

        handleUpdate(row) {
            this.reset()
            const id = row.id || this.ids
            getExcelExportTask(id).then(response => {
                this.form = response.data
                this.open = true
                this.title = '修改excel导出任务'
            })
        },

        submitForm() {
            this.$refs['form'].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateExcelExportTask(this.form).then(response => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addExcelExportTask(this.form).then(response => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },

        handleDelete(row) {
            const ids = row.id || this.ids
            this.$modal.confirm('是否确认删除excel导出任务编号为"' + ids + '"的数据项？').then(function() {
                return delExcelExportTask(ids)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess('删除成功')
            }).catch(() => {
            })
        },

        handleExport() {
            this.download('excel/excelExportTask/export', {
                ...this.queryParams
            }, `excelExportTask_${new Date().getTime()}.xlsx`)
        },
        // 下载
        handleDownload(row) {
            if (row.status === '3') {
                this.downloadGet(`business/excel-export/download?taskId=${row.id}`, `任务_${new Date().getTime()}.xlsx`)
            } else {
                this.$message.error('任务未完成')
            }
        }
    }
}
</script>
